package com.foreveross.util.redis;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;


public class ProxyIpJedisTools {

	private static Jedis jedis;
	private static Logger log = LoggerFactory
			.getLogger(ProxyIpJedisTools.class);

//	public static synchronized ProxyIpModel get(String key) {
//		try {
//			jedis = JedisUtil.getResource();
//			String value = jedis.rpop(key);
//			return RedisSerializeUtil.deserializeProxyIpModel(value);
//		} catch (Exception e) {
//			log.info(String.format("获取Key:[%s]失败!", key), e);
//			return null;
//		} finally {
//			JedisUtil.returnResource(jedis);
//		}
//	}

//	public static synchronized Long put(String key, List<ProxyIpModel> proxyList) {
//		try {
//			jedis = JedisUtil.getResource();
//
//			if (proxyList.size() > 0) {
//				// 打开事务
//				Pipeline pipeline = jedis.pipelined();
//				pipeline.multi();
//
//				for (ProxyIpModel value : proxyList) {
//					
//					//pipeline.hset(key, "", RedisSerializeUtil.serializeProxyIpModel(value));
////					pipeline.hset(key,
////							RedisSerializeUtil.serializeProxyIpModel(value));
//					pipeline.lpush(key,RedisSerializeUtil.serializeProxyIpModel(value));
//				}
//				// 提交
//				pipeline.exec();
//
//			}
//			return 1L;
//		} catch (Exception e) {
//			log.info(String.format("往Redis 中有些话 Key:[%s]时失败!", key), e);
//			return null;
//		} finally {
//			JedisUtil.returnResource(jedis);
//		}
//	}

	public static synchronized boolean counterLog(String key, int num) {
		try {
			jedis = JedisUtil.getResource();
			// System.out.println("key::::"+ key);
			// System.out.println("jedis.exists(key)"+ jedis.exists(key));
			if (jedis.exists(key)) {
				// System.out.println("count"+ jedis.get(key));
				String count = jedis.get(key);

				int total = Integer.parseInt(count) + num;
				jedis.set(key, String.valueOf(total));

			} else {
				jedis.set(key, "0");
			}

			return true;
		} catch (Exception e) {

			log.error(String.format("往Redis Key:[%s]时修改失败!", key), e);
			return false;
		} finally {
			JedisUtil.returnResource(jedis);
		}
	}

	public static synchronized long countHotPoolSize(String channel) {
		String key = RedisKeyTools.generateHotPoolKey(channel);
		try {
			long size = jedis.llen(key);
			return size;
		} catch (Exception e) {
			log.error(String.format("往Redis Key:[%s]时修改失败!", key), e);
			return 0;
		} finally {
			JedisUtil.returnResource(jedis);
		}

	}

}
